//---------------------------------------------------------
// AirCrash Game Engine
//	 v1.0
//
// (c) AirCrash
//----------------------------------------------------------

#pragma once

class GameEngine;
class Camera;
#include "Light.h"
#include <map>

#define DXDEVICE (DXDevice::GetSingleton())
#define GETDEVICE (DXDEVICE->GetDevice())

class DXDevice
{
public:
	// Destructor en Singleton
	virtual ~DXDevice();
	static DXDevice* GetSingleton();

	// Cleanup DX device
	void Cleanup();
	
	// Render the scene + update
	void Render();
	void Tick(float dTime);

	// Get the device
	ID3D10Device* GetDevice() const;

	// Get matrixen
	D3DXMATRIX GetViewProj() const;
	D3DXMATRIX GetSpriteViewProj() const; // Special for the sprites
	void GetMatrixen(D3DXMATRIX &world, D3DXMATRIX &view, D3DXMATRIX &proj);
	void GetMatrixen(D3DXMATRIX &view, D3DXMATRIX &proj);
	
	// Camera
	void SetupCamera(D3DXVECTOR3 pos, D3DXVECTOR3 target);
	Camera* GetCamera();
	void SetCamera(const D3DXVECTOR3& pos, const D3DXVECTOR3& lookat);
	
	// Font
	const D3DXCOLOR& GetColor() const;
	void SetColor(const D3DXCOLOR& kleur);
	void SetFont(const tstring& type, int size = 12, bool bold = false, bool cursief = false, bool underline = false);
	void SetDefaultFont();

	// DrawText
	void DrawText(const tstring& text, int x, int y) const;
	void DrawText(const tstring& text, int x, int y, int width, int height) const;
	void DrawText(const tstring& text, int x, int y, int width, int height, int align) const;


	// FPS
	void ShowFps();

	// Init DXDevice
	HRESULT Init();

	// Resizing
	void OnResize();

	// Blending
	void EnableBlend();
	void DisableBlend() const;

	// other
	void ResetRenderTargets();
	void ClearDepth();
	void ClearStencil();
	Light& GetLight();

private:
	// Private constructor
	DXDevice();

	// Init the DXDevice
	HRESULT InitFont();
	HRESULT InitSwapChain();
	HRESULT InitDepthStencil();
	HRESULT InitRenderTargetView();
	HRESULT InitBlend();
	void SetViewPort();


	static DXDevice* m_DxDevicePtr; // Singleton
	ID3D10Device* m_D3D10DevicePtr; // DXD10Device
	IDXGISwapChain* m_SwapChainPtr; // SmapChain
	D3D10_DRIVER_TYPE m_driverType;
	ID3D10RenderTargetView* m_RenderTargetViewPtr; // RenderTarget View
	ID3D10Texture2D* m_DepthStencilPtr; // Depth stencil Buffer
	ID3D10DepthStencilView* m_DepthStencilViewPtr; // Depth Stencil view
	Light m_Light;
	
	//Font
	map<tstring, ID3DX10Font*> m_Font;
	ID3DX10Font* m_ActiveFont;
	ID3DX10Sprite *m_FontSpritePtr;
	D3DXCOLOR m_FontColor;

	// View Projection
	D3DXMATRIXA16 m_ViewProj, m_SpriteViewProj;
	D3DXMATRIX m_World, m_View, m_Proj;
	
	// Rasterizer
	ID3D10RasterizerState* m_RSPtr;

	// Camera
	Camera* m_CameraPtr;

	// Performance Counter
	LARGE_INTEGER			m_PerformanceFrequency;
	LARGE_INTEGER			m_PrevTime;
	LARGE_INTEGER			m_CurrentTime;
	int m_FPSCounter,m_FPSTotal,m_FPS;
	int m_AvgFPS;

	// Blending
	ID3D10BlendState* m_BlendState;
	DXDevice(const DXDevice &t);
	DXDevice& operator= (const DXDevice &t);
};